package com.xiaolong.web;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class BaseController {
	/**
	 * 统一异常处理
	 * @param ex
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@ExceptionHandler(Exception.class)
	public void exceptionHandler(Exception ex, HttpServletResponse response, HttpServletRequest request) throws IOException{
		log.info("exceptionHandler:"+ex.getMessage());
		response.setContentType("text/html;charset=utf-8");  
		response.encodeRedirectURL("/error/404");
		response.sendRedirect("/error/404");
        //response.getWriter().write(new Gson().toJson(ex.getMessage()));
	}
	
	@RequestMapping(value="/error/{page}",method=RequestMethod.GET)
	public ModelAndView error(@PathVariable String page){
		return new ModelAndView("/views/"+page);
	}
}
